#!/bin/sh /etc/rc.common

START=99
STOP=15

CONFIG=passwall
APP_FILE=/usr/share/${CONFIG}/app.sh
LOCK_FILE=/var/lock/${CONFIG}.lock

lock() {
	if [ -s ${LOCK_FILE} ]; then
		SPID=$(cat ${LOCK_FILE})
		if [ -e /proc/${SPID}/status ]; then
			#echo "脚本已经在运行，不重复运行，退出."
			exit 1
		fi
		cat /dev/null > ${LOCK_FILE}
	fi
	echo $$ > ${LOCK_FILE}
}

unlock() {
	rm -rf ${LOCK_FILE}
}

boot() {
	$APP_FILE boot
}

start() {
	lock
	$APP_FILE start
	unlock
}

stop() {
	lock
	$APP_FILE stop
	unlock
}

restart() {
	lock
	$APP_FILE stop
	$APP_FILE start
	unlock
}

disable() {
	rm -f "$IPKG_INSTROOT"/etc/rc.d/S??zzz_${CONFIG}
	rm -f "$IPKG_INSTROOT"/etc/rc.d/K??zzz_${CONFIG}
}

enable() {
	err=1
	[ "$START" ] && \
		ln -sf "../init.d/${CONFIG}" "$IPKG_INSTROOT/etc/rc.d/S${START}zzz_${CONFIG}" && \
		err=0
	[ "$STOP" ] && \
		ln -sf "../init.d/${CONFIG}" "$IPKG_INSTROOT/etc/rc.d/K${STOP}zzz_${CONFIG}" && \
		err=0
	return $err
}

enabled() {
	[ -x "$IPKG_INSTROOT/etc/rc.d/S${START}zzz_${CONFIG}" ]
}
